package com.hivekion.common.file;


import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.util.Zip4jConstants;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class ZipUtils {

    /**
     * 压缩指定路径的文件
     *
     * @param srcFilePath     待压缩文件路径
     * @param zipPathFileName zip文件全路径名
     * @param password        加密密码
     * @return
     */
    public static boolean zipFile(String srcFilePath, String zipPathFileName, String password) {
        try {
            // 生成的压缩文件
            ZipFile zipFile = new ZipFile(zipPathFileName);
            ZipParameters parameters = new ZipParameters();
            // 压缩级别
            parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
            parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
            if (!StringUtils.isEmpty(password)) {
                parameters.setEncryptFiles(true);
                parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);
                parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);
                parameters.setPassword(password);
            }
            // 要打包的文件夹
            File currentFile = new File(srcFilePath);
            File[] fs = currentFile.listFiles();
            //遍历文件夹下所有的文件、文件夹
            for (File f : fs) {
                if (f.isDirectory()) {
                    zipFile.addFolder(f.getPath(), parameters);
                } else {
                    zipFile.addFile(f, parameters);
                }
            }
            return true;
        } catch (ZipException e) {
            return false;
        }
    }

    /**
     * @param zipFileFullName zip文件所在的路径名
     * @param filePath        解压到的路径
     * @param password        需要解压的密码
     * @return
     */
    public static boolean unZipFile(String zipFileFullName, String filePath, String password) {
        try {
            ZipFile zipFile = new ZipFile(zipFileFullName);
            // 如果解压需要密码
            if (StringUtils.isNotEmpty(password) && zipFile.isEncrypted()) {
                zipFile.setPassword(password);
            }
            zipFile.extractAll(filePath);
            return true;
        } catch (ZipException e) {
            return false;
        }
    }

    /**
     * 添加文件到压缩文件中
     *
     * @param zipFullFileName  zip文件所在路径及全名
     * @param fullFileNameList 待添加的文件全路径集合
     * @param rootFolderInZip  在压缩文件里的文件夹名
     * @return
     */
    public static boolean addFilesToZip(String zipFullFileName, List<String> fullFileNameList, String rootFolderInZip) {
        try {
            ZipFile zipFile = new ZipFile(zipFullFileName);
            ArrayList addFiles = new ArrayList<>();
            for (String fileName : fullFileNameList) {
                addFiles.add(new File(fileName));
            }
            ZipParameters parameters = new ZipParameters();
            parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
            parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
            if (StringUtils.isNotEmpty(rootFolderInZip)) {
                if (rootFolderInZip.endsWith("/") == false) {
                    rootFolderInZip = rootFolderInZip + "/";
                }
                parameters.setRootFolderInZip(rootFolderInZip);
            }
            zipFile.addFiles(addFiles, parameters);
            return true;
        } catch (ZipException e) {
            return false;
        }
    }

    /**
     * 从压缩文件中删除路径
     *
     * @param zipFullFileName
     * @param fileName
     * @return
     */
    public static boolean deleteFileInZip(String zipFullFileName, String fileName) {
        try {
            ZipFile zipFile = new ZipFile(zipFullFileName);
            zipFile.removeFile(fileName);
            return true;
        } catch (ZipException e) {
            return false;
        }
    }
}
